<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avator_box">
        <img src="@/assets/image/logo.jpg" alt />
      </div>
      <el-form ref="login_form" :model="loginForm" class="login_form" :rules="loginRules">
        <el-form-item prop="username">
          <el-input ref="username" prefix-icon="iconfont icon-icontouxiang" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" v-model="loginForm.password" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button type="info" @click="resetForm('login_form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { CommonApi } from '@/api/apiConfig'
export default {
  name: 'login',
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
      // else if (!(/^[a-z|A-Z|0-9]{6}$/).test(value)) {
      //   callback(new Error('密码由字母、数字六位组成'))
      // }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.loginFormFocus()
  },
  methods: {
    loginFormFocus () {
      this.$refs.username.focus()
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.loginFormFocus()
    },
    submitForm () {
      this.$refs.login_form.validate((valid) => {
        if (!valid) return false
        const params = {
          username: this.loginForm.username,
          password: this.loginForm.password
        }
        CommonApi.login(params).then(res => {
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message({
            message: '登录成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.$jsCookie.set('token', res.data.token)
              this.$router.push({
                path: '/home'
              })
            }
          })
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    left: 50%;
    top: 50%;
    margin-left: -225px;
    margin-top: -150px;
    //或者使用
    // transform: translate(-50%, -50%);
    .avator_box {
      position: absolute;
      height: 130px;
      width: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_form {
      position: absolute;
      width: 100%;
      bottom: 0;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
